package kk.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.github.pagehelper.PageInfo;
import kk.domain.Product;
import kk.service.OrdersService;
import kk.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RequestMapping("/product")
@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private OrdersService ordersService;

    /**
     * 查询所有产品
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/findAll")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public ModelAndView findAll(Integer pageNum,Integer pageSize) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        //查询出所有产品
        List<Product> productList = productService.findAll(pageNum, pageSize);
        //封装成页对象
        PageInfo pageInfo=new PageInfo(productList);
        modelAndView.addObject("pageInfo",pageInfo);
        //设置跳转页面
        modelAndView.setViewName("product-list");
        return modelAndView;
    }

    /**
     * 添加产品
     *
     * @param product
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String save(Product product) throws Exception {
        productService.save(product);
        return "redirect:/product/findAll?pageNum=1&pageSize=4";
    }

    /**
     * 删除指定id的产品,并删除包含该产品的订单
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String delete(Integer id) throws Exception {
        //删除产品前先把相应的订单删除掉
        ordersService.deleteByProductId(id);
        productService.deleteById(id);
        return "redirect:/product/findAll?pageNum=1&pageSize=4";
    }

    /**
     * 根据id查询出产品
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public ModelAndView findById(Integer id) throws Exception {
        Product product = productService.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("product", product);
        modelAndView.setViewName("product-update");
        return modelAndView;
    }

    /**
     * @param product
     * @throws Exception
     */
    @RequestMapping("/update")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String update(Product product) throws Exception {
        productService.update(product);
        return "redirect:/product/findAll?pageNum=1&pageSize=4";
    }

    /**
     * 删除多个product，同时删除这些产品所关联的订单
     * @param ids
     */
    @RequestMapping("/deleteByIds")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public String deleteByIds(int[] ids) throws Exception {
        for (int id : ids) {
            ordersService.deleteByProductId(id);
        }
        productService.deleteByIds(ids);
        return "redirect:/product/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/findAllProductForOrders")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public @ResponseBody List<Product> findAllProductForOrders() throws Exception {
        List<Product> productList = productService.findAll();
        return productList;
    }


    @RequestMapping("/findProductByLevel")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public @ResponseBody List<Product> findProductByLevel(String level) throws Exception{
        List<Product> productList = productService.findProductByLevel(level);
        return productList;
    }

    @RequestMapping("/findProductByName")
    @PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
    public ModelAndView findProductByName(Integer pageNum,Integer pageSize,String productName) throws Exception {
        String newName="%"+productName+"%";
        List<Product> productList = productService.findByName(pageNum, pageSize, newName);
        PageInfo pageInfo=new PageInfo(productList);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("productName",productName);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("product-list");
        return modelAndView;
    }
}
